package cn.ssm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ssm.pojo.Admin;
import cn.ssm.service.AdminService;

/**
 * 会员控制类
 * @author 李金栋
 *
 */
@Controller
@RequestMapping("/admin")
public  class AdminController  {

	@Autowired
	AdminService  adminService;

		/**
		 * 查询所有用户
		 * createdBy-创建角色人的角色名称
		 * roleId-角色名称
		 * @return
		 */
		@RequestMapping("/getAdminList")
		@ResponseBody
		public Object getAdminList(Integer nowPage, Integer size) {
			//接受的参数
			Map<String, Object> paramas = new HashMap<String, Object>();
			if(nowPage !=null || size !=null){
				paramas.put("start", (nowPage-1)*size);
				paramas.put("size", size);
			}
			
			//返回的结果
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("success", false);
			
			try{
				List<Admin> adminList = adminService.getAdminList(paramas);
				map.put("result", adminList);
				map.put("message", "查询用户成功");
				map.put("success", true);
			}catch(Exception e){
				map.put("result", "查询用户出现异常");
			}
			return map;
		}
		
		//添加用户
		@RequestMapping("/addAdmin")
		@ResponseBody
		public Object addAdmin(Admin addAdmin){
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("success",false);
			
			try {
				if(addAdmin!=null){
					Admin admin = new Admin();
					admin.setAdminName(addAdmin.getAdminName());
					admin.setRoleId(addAdmin.getRoleId());
					admin.setAdminCode(addAdmin.getAdminCode());
					admin.setAdminPassword(addAdmin.getAdminPassword());
					admin.setCreatedBy(addAdmin.getId());
					admin.setSex(addAdmin.getSex());
					admin.setPhone(addAdmin.getPhone());
					admin.setCreateDate(new Date());
					
					int addAdmin2 = adminService.addAdmin(admin);
					
					if(addAdmin2>0){
						map.put("success", true);
						map.put("message", "添加用户成功");

					}
				}
				
			} catch (Exception e) {
				map.put("result", "添加用户出现异常");
				e.printStackTrace();
			}
			return map;
		}
		
		//修改用户
		@RequestMapping("/updateAdmin")
		@ResponseBody
		public Object updateAdmin(Admin addAdmin){
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("success",false);
			
			try {
				if(addAdmin!=null){
					Admin admin = new Admin();
					admin.setId(addAdmin.getId());
					admin.setAdminName(addAdmin.getAdminName());
					admin.setRoleId(addAdmin.getRoleId());
					admin.setAdminCode(addAdmin.getAdminCode());
					admin.setAdminPassword(addAdmin.getAdminPassword());
					//admin.setCreatedBy(addAdmin.getId());
					admin.setSex(addAdmin.getSex());
					admin.setPhone(addAdmin.getPhone());
					admin.setCreateDate(new Date());
					
					int addAdmin2 = adminService.updateAdmin(admin);
					
					if(addAdmin2>0){
						map.put("success", true);
						map.put("message", "修改用户成功");
					}
				}
			} catch (Exception e) {
				map.put("result", "修改用户出现异常");
				e.printStackTrace();
			}
			return map;
		}
		
		//删除用户
		@RequestMapping("/deleteAdmin")
		@ResponseBody
		public void deleteAdmin(Admin admins){
			Admin admin = new Admin();
			admin.setId(admins.getId());
			
			adminService.deleteAdmin(admin);
			
		}
		
		//查询admin总数
		@RequestMapping("/getAdminCount")
		@ResponseBody
		public int getAdminCount(){
			return adminService.getAdminCount();
		}
		
		//根据adminName查询管理员的权限
		@RequestMapping("/selectAdmin")
		@ResponseBody
		public int selectAdmin(@Param("adminName")String adminName){
			System.out.println(adminName);
			int count = adminService.selectAdmin(adminName);
			return count;
		}
		
		/**
		 * 验证
		 * @param user
		 * @return
		 */
		@RequestMapping("/getAdmin")
		@ResponseBody
		public Object getAdmin(Admin admin){
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("success",false);
			try {
				if (admin != null) {
					List<Admin> admins = adminService.getAdmin(admin);
					if (admins.isEmpty()) {
						map.put("success", true);
						map.put("message", "账号密码可以注册");
					}else{
						map.put("success", false);
						map.put("message", "已注册过，请更换");
					}
				} 
			} catch (Exception e) {
				map.put("result", "注册用户出现异常");
				e.printStackTrace();
			}
			return map;
		}
		
		/**
		 * 搜索id
		 * @param user
		 * @return
		 */
		@RequestMapping("/getAdmins")
		@ResponseBody
		public Object getAdmins(Admin admin){
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("success",false);
			try {
				if (admin != null) {
					List<Admin> admins = adminService.getAdmins(admin);
					if (admins.size()!=0) {
						map.put("success", true);
						map.put("result", admins);
					}else{
						map.put("success", false);
					}
				} 
			} catch (Exception e) {
				map.put("result", "注册用户出现异常");
				e.printStackTrace();
			}
			return map;
		}
		
		/**
		 * 验证是否修改用户
		 * @param user
		 * @return
		 */
		@RequestMapping("/getAdminCheck")
		@ResponseBody
		public Object getAdminCheck(Admin admin){
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("success",false);
			try {
				if (admin != null) {
					List<Admin> admins = adminService.getAdminCheck(admin);
					if (admins.size()==0) {
						map.put("success", true);
						map.put("message", "可以修改");
					}else{
						map.put("success", false);
						map.put("message", "并没有可修改的啊");
					}
				} 
			} catch (Exception e) {
				map.put("result", "注册用户出现异常");
				e.printStackTrace();
			}
			return map;
		}
}
